package com.boot.ys.mybatisplus.Test;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 描述
 *
 * @Author yangsheng
 * @Date 2025/2/7 17:04
 */
public class demo02 {
    public static void main(String[] args) {

        try {

            // 使用 HashMap 存储输入地址和输出地址
            Map<String, String> directories = new HashMap<>();

            // 添加目录信息key：输入地址，value：输出地址
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SpringBoot0.0", "E:\\BaiduNetdiskDownload\\ymddok\\SpringBoot0.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SpringBoot1.0", "E:\\BaiduNetdiskDownload\\ymddok\\SpringBoot1.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SpringBoot2.0", "E:\\BaiduNetdiskDownload\\ymddok\\SpringBoot2.0");

            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SpringBoot3.0", "E:\\BaiduNetdiskDownload\\ymddok\\SpringBoot3.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SpringBoot3.1", "E:\\BaiduNetdiskDownload\\ymddok\\SpringBoot3.1");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SpringBoot4.0", "E:\\BaiduNetdiskDownload\\ymddok\\SpringBoot4.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SpringBoot4.1", "E:\\BaiduNetdiskDownload\\ymddok\\SpringBoot4.1");

            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SSM0.0", "E:\\BaiduNetdiskDownload\\ymddok\\SSM0.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SSM1.0", "E:\\BaiduNetdiskDownload\\ymddok\\SSM1.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SSM1.1", "E:\\BaiduNetdiskDownload\\ymddok\\SSM1.1");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SSM2.0", "E:\\BaiduNetdiskDownload\\ymddok\\SSM2.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SSM3.0", "E:\\BaiduNetdiskDownload\\ymddok\\SSM3.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\SSM3.1", "E:\\BaiduNetdiskDownload\\ymddok\\SSM3.1");

            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\Wechat1.0", "E:\\BaiduNetdiskDownload\\ymddok\\Wechat1.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\Wechat2.0", "E:\\BaiduNetdiskDownload\\ymddok\\Wechat2.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\Wechat3.0", "E:\\BaiduNetdiskDownload\\ymddok\\Wechat3.0");
            directories.put("E:\\BaiduNetdiskDownload\\ymdd\\Wechat4.0", "E:\\BaiduNetdiskDownload\\ymddok\\Wechat4.0");






            // 遍历 HashMap 并调用压缩方法
            for (Map.Entry<String, String> entry : directories.entrySet()) {
                String sourceDirPath = entry.getKey();
                String targetDirPath = entry.getValue();

                File sourceDir = new File(sourceDirPath);
                File targetDir = new File(targetDirPath);

                // 调用压缩方法
                float quality = 0.6f; // 设置压缩质量


                traverseAndCompressDirectory(sourceDir, targetDir, quality);
            }


            System.out.println("Compression completed.");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    // 压缩图片的方法
    public static void compressImage(File inputFile, File outputFile, float quality) throws IOException {
        // 读取图片
        BufferedImage image = ImageIO.read(inputFile);

        // 获取JPEG写入器
        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = writers.next();

        // 设置输出
        ImageOutputStream ios = ImageIO.createImageOutputStream(new FileOutputStream(outputFile));
        writer.setOutput(ios);

        // 设置压缩质量
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality); // 0到1之间，1为无损

        // 写入图片
        writer.write(null, new javax.imageio.IIOImage(image, null, null), param);

        // 关闭资源
        ios.close();
        writer.dispose();
    }

    // 遍历文件夹并压缩图片
    public static void traverseAndCompressDirectory(File sourceDir, File targetDir, float quality) throws IOException {
        if (!sourceDir.exists() || !sourceDir.isDirectory()) {
            System.out.println("Source directory does not exist or is not a directory.");
            return;
        }

        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        File[] files = sourceDir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // 如果是文件夹，递归处理
                    File newTargetDir = new File(targetDir, file.getName());
                    traverseAndCompressDirectory(file, newTargetDir, quality);
                } else if (file.isFile() && (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".jpeg"))) {
                    // 如果是图片文件，进行压缩
                    File newTargetFile = new File(targetDir, file.getName());
                    compressImage(file, newTargetFile, quality);
                    System.out.println("Compressed: " + file.getAbsolutePath() + " -> " + newTargetFile.getAbsolutePath());
                }
            }
        }
    }



}

